package me.algo.lst;

import java.util.Objects;

/**
 * @author guozheng
 * @date 2024/4/22
 */
public class L5 {


    /**
     * 328. 奇偶链表
     * @param head
     * @return
     */
    public static ListNode oddEvenList(ListNode head) {
        if (Objects.isNull(head)) {
            return null;
        }
        int index = 1;
        ListNode odd = new ListNode();
        ListNode ccur = odd;
        ListNode even = new ListNode();
        ListNode ecur = even;
        ListNode cur = head;
        while (cur != null) {
            if (index % 2 == 1) {
                ccur.next = cur;
                ccur = ccur.next;
                cur = cur.next;
                ccur.next = null;
            } else {
                ecur.next = cur;
                ecur = ecur.next;
                cur = cur.next;
                ecur.next = null;
            }
            index++;
        }
        ccur.next = even.next;
        return odd.next;
    }
}
